package com.djzhou.gmms.message.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.dao.base.impl.BaseDAOImpl;
import com.djzhou.gmms.model.email.TbEmail;
import com.djzhou.gmms.model.message.TbMessage;
import com.djzhou.gmms.dao.message.MessageDAO;

public class MessageAction extends BaseAction<TbMessage>{
	private TbMessage message;
	private List<TbMessage> list;
	private Integer messageId;
	
	private String receiverNameStr;
	private String receiverIdStr;
	
	private TbMessage messageAjax;
	
	/**
	 * 已读回执
	 * @return
	 */
	public String readMessage(){
		message=messageService.queryById(messageId);
		message.setMessageState(1);
		messageService.update(message);
		return SUCCESS;
	}
	/**
	 * 监听消息
	 * @return
	 */
	public String listenMessage(){
		list=messageService.queryMessageByUserId(queryUser().getUserId());
		if(list!=null && list.size()>0){
			msg=list.get(0).getSender().getRealName()+"对您说："+list.get(0).getMessageContent();
			message=list.get(0);
		}else{
			msg=null;
		}
		return SUCCESS;
	}
	/**
	 * 更新消息提示
	 * @return
	 */
	public String updateMessageTip(){
		message=messageService.queryById(messageId);
		message.setMessageTip(1);
		messageService.update(message);
		return SUCCESS;
	}
	
	/**
	 * 查询收件箱
	 * @return
	 */
	public String queryReceiver(){
		if(loadData==null)return SUCCESS;
		pageBean=messageService.queryReceiver(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = messageService.queryReceiver(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbMessage>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}
	
	/**
	 * 查询发件箱
	 * @return
	 */
	public String querySender(){
		if(loadData==null)return SUCCESS;
		pageBean=messageService.querySender(queryUser().getUserId(), pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = messageService.querySender(queryUser().getUserId(), pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbMessage>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}
	
	public String deleteReceiver(){
		message=messageService.queryById(messageId);
		message.setReceiveDelete(1);
		messageService.update(message);
		return SUCCESS;
	}
	
	public String deleteSender(){
		message=messageService.queryById(messageId);
		message.setSendDelete(1);
		messageService.update(message);
		return SUCCESS;
	}
	
	
	
	@Override
	public String delete() {
		messageService.delete(messageId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("用户消息",queryUser(),"删除",messageId,"TbMessage",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		messageService.update(message);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("用户消息",queryUser(),"编辑",message.getMessageId(),"TbMessage",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		pageBean=messageService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean = messageService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbMessage>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(message!=null){
			String[] array=receiverIdStr.split(",");
			for(String idStr:array){
				Integer id=Integer.parseInt(idStr);
				message.setMessageState(0);
				message.setReceiveDelete(0);
				message.setSendDelete(0);
				message.setMessageTip(0);
				message.setReceiver(userService.queryById(id));
				message.setSender(queryUser());
				message.setSendDate(new Date());
				messageService.add(message);
			}
			msg="已回复给"+message.getReceiver().getRealName();
		}
		setCurrentPage(currentPage);
		logService.addLog("用户消息",queryUser(),"添加",message.getMessageId(),"TbMessage",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		setCurrentPage(currentPage);
		message=messageService.queryById(messageId);
		return SUCCESS;
	}

	public TbMessage getMessage() {
		return message;
	}

	public void setMessage(TbMessage message) {
		this.message = message;
	}

	public List<TbMessage> getList() {
		return list;
	}

	public void setList(List<TbMessage> list) {
		this.list = list;
	}

	public Integer getMessageId() {
		return messageId;
	}

	public void setMessageId(Integer messageId) {
		this.messageId = messageId;
	}

	public String getReceiverNameStr() {
		return receiverNameStr;
	}

	public void setReceiverNameStr(String receiverNameStr) {
		this.receiverNameStr = receiverNameStr;
	}

	public String getReceiverIdStr() {
		return receiverIdStr;
	}

	public void setReceiverIdStr(String receiverIdStr) {
		this.receiverIdStr = receiverIdStr;
	}
	public TbMessage getMessageAjax() {
		return messageAjax;
	}
	public void setMessageAjax(TbMessage messageAjax) {
		this.messageAjax = messageAjax;
	}
	
}
